#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    client = new QMqttClient;
    //绑定连接成功信号
    connect(client, &QMqttClient::connected, this, &Widget::connectSuccess);
    connect(client, &QMqttClient::disconnected, this, &Widget::connectError);
    //绑定连接的ip

        client->setHostname("mqtt.yyzlab.com.cn");
    //绑定端口
        client->setPort(1883);
//        client->setPort(9002);
    //连接服务器
    client->connectToHost();


}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    //发布消息
    char buf[20];
    sprintf(buf,"{\"lamp\":%s,\"id\":0}",ui->lineEdit->text().toLatin1().data());

    //    client->publish(QString("IceBear/1688437874061/AIOTSIM2APP"), buf);
    client->publish(QString("1688441470336/APP2AIOTSIM"), buf);
}

void Widget::connectSuccess(){
    qDebug()<<"connect success";
}
void Widget::connectError(){
    qDebug()<<"connect disabled";
}
void Widget::Receive(QByteArray buf)
{
    qDebug()<<"receive ";
    qDebug()<<QString(buf);
}

void Widget::on_pushButton_2_clicked()
{
    connect(client, &QMqttClient::messageReceived, this, &Widget::Receive);

    //    client->subscribe(QString("IceBear/1688437874061/APP2AIOTSIM"));
    client->subscribe(QString("1688441470336/AIOTSIM2APP"));
}
